/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.zimbra.common.localconfig.DebugConfig;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.common.util.memcached.MemcachedMap;
/*     */ import com.zimbra.common.util.memcached.MemcachedSerializer;
/*     */ import com.zimbra.common.util.memcached.ZimbraMemcachedClient;
/*     */ import com.zimbra.cs.memcached.MemcachedConnector;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class FoldersTagsCache
/*     */ {
/*  37 */   private static FoldersTagsCache sTheInstance = new FoldersTagsCache();
/*     */   
/*     */   private MemcachedMap<FoldersTagsCacheKey, FoldersTags> mMemcachedLookup;
/*     */   
/*  41 */   public static FoldersTagsCache getInstance() { return sTheInstance; }
/*     */   
/*     */   FoldersTagsCache() {
/*  44 */     ZimbraMemcachedClient memcachedClient = MemcachedConnector.getClient();
/*  45 */     FoldersTagsSerializer serializer = new FoldersTagsSerializer();
/*  46 */     this.mMemcachedLookup = new MemcachedMap(memcachedClient, serializer, false);
/*     */   }
/*     */   
/*     */   static class FoldersTags { private static final int DATA_VERSION = 1;
/*     */     private MetadataList mFolders;
/*     */     private MetadataList mTags;
/*     */     private static final String FN_DATA_VERSION = "dv";
/*     */     private static final String FN_FOLDERS = "folders";
/*     */     private static final String FN_TAGS = "tags";
/*     */     
/*  56 */     public FoldersTags(List<Folder> folders, List<Tag> tags) { this.mFolders = new MetadataList();
/*  57 */       for (Folder f : folders) {
/*  58 */         this.mFolders.add(f.serializeUnderlyingData());
/*     */       }
/*  60 */       this.mTags = new MetadataList();
/*  61 */       for (Tag t : tags) {
/*  62 */         this.mTags.add(t.serializeUnderlyingData());
/*     */       }
/*     */     }
/*     */     
/*     */     private FoldersTags(MetadataList folders, MetadataList tags) {
/*  67 */       this.mFolders = folders;
/*  68 */       this.mTags = tags;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */     public Metadata encode()
/*     */     {
/*  76 */       Metadata meta = new Metadata();
/*  77 */       meta.put("dv", 1L);
/*  78 */       meta.put("folders", this.mFolders);
/*  79 */       meta.put("tags", this.mTags);
/*  80 */       return meta;
/*     */     }
/*     */     
/*     */     public static FoldersTags decode(Metadata meta) throws ServiceException {
/*  84 */       int ver = (int)meta.getLong("dv", 0L);
/*  85 */       if (ver != 1) {
/*  86 */         ZimbraLog.mailbox.info("Ignoring cached folders/tags with stale data version");
/*  87 */         return null;
/*     */       }
/*  89 */       MetadataList folders = meta.getList("folders");
/*  90 */       MetadataList tags = meta.getList("tags");
/*  91 */       return new FoldersTags(folders, tags);
/*     */     }
/*     */     
/*     */     public List<Metadata> getFolders() {
/*  95 */       List<Metadata> toRet = new ArrayList();
/*  96 */       List list = this.mFolders.asList();
/*  97 */       for (Object obj : list) {
/*  98 */         if ((obj instanceof Metadata))
/*  99 */           toRet.add((Metadata)obj);
/*     */       }
/* 101 */       return toRet;
/*     */     }
/*     */     
/*     */     public List<Metadata> getTags() {
/* 105 */       List<Metadata> toRet = new ArrayList();
/* 106 */       List list = this.mTags.asList();
/* 107 */       for (Object obj : list) {
/* 108 */         if ((obj instanceof Metadata))
/* 109 */           toRet.add((Metadata)obj);
/*     */       }
/* 111 */       return toRet;
/*     */     }
/*     */   }
/*     */   
/*     */   private static class FoldersTagsSerializer
/*     */     implements MemcachedSerializer<FoldersTagsCache.FoldersTags>
/*     */   {
/*     */     public Object serialize(FoldersTagsCache.FoldersTags value)
/*     */     {
/* 120 */       return value.encode().toString();
/*     */     }
/*     */     
/*     */     public FoldersTagsCache.FoldersTags deserialize(Object obj) throws ServiceException
/*     */     {
/* 125 */       Metadata meta = new Metadata((String)obj);
/* 126 */       return FoldersTagsCache.FoldersTags.decode(meta);
/*     */     }
/*     */   }
/*     */   
/*     */   public FoldersTags get(Mailbox mbox) throws ServiceException {
/* 131 */     FoldersTagsCacheKey key = new FoldersTagsCacheKey(mbox.getAccountId());
/* 132 */     return (FoldersTags)this.mMemcachedLookup.get(key);
/*     */   }
/*     */   
/*     */   public void put(Mailbox mbox, FoldersTags foldersTags) throws ServiceException {
/* 136 */     if (DebugConfig.disableFoldersTagsCache) {
/* 137 */       return;
/*     */     }
/* 139 */     FoldersTagsCacheKey key = new FoldersTagsCacheKey(mbox.getAccountId());
/* 140 */     this.mMemcachedLookup.put(key, foldersTags);
/*     */   }
/*     */   
/*     */   public void purgeMailbox(Mailbox mbox) throws ServiceException {
/* 144 */     if (DebugConfig.disableFoldersTagsCache) {
/* 145 */       return;
/*     */     }
/* 147 */     FoldersTagsCacheKey key = new FoldersTagsCacheKey(mbox.getAccountId());
/* 148 */     this.mMemcachedLookup.remove(key);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/FoldersTagsCache.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */